package com.ruoyi.web.controller.vantController.customerController;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.customer.CustomerLogin;
import com.ruoyi.system.domain.distribution.CashManagement;
import com.ruoyi.system.service.vantservice.customerervice.CashManagerMentService;
import com.ruoyi.system.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.util.List;

/**
 * 前端我的收益接口
 */
@RestController
@RequestMapping("api/cashManagerment")
public class ApiCashManagermentController {
    @Autowired
    private TokenUtils tokenUtils;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private CashManagerMentService cashManagerMentService;

    /**
     * 根据用户id查询收益
     * @param request
     * @return
     */
    @GetMapping
    public AjaxResult getManagerment(){
        CustomerLogin loginCustomer = tokenUtils.getLoginCustomer(request);
        if(loginCustomer!=null){
            List<CashManagement> cashManageMnets = cashManagerMentService.getCashManagementListByInviterId(loginCustomer.getCustomerId());
            if(cashManageMnets!=null && cashManageMnets.size()>0){
                for(int i = 0;i<cashManageMnets.size();i++){
                    DecimalFormat df = new DecimalFormat("#.##");
                    cashManageMnets.get(i).setBalance(Double.parseDouble(df.format(cashManageMnets.get(i).getDepositMoney()+cashManageMnets.get(i).getBalance())));
                    cashManageMnets.get(i).setTotalNum(Double.parseDouble(df.format(cashManageMnets.get(i).getDepositMoney() +cashManageMnets.get(i).getTotalNum())) );
                    if(i>=1){
                        cashManageMnets.get(i).setBalance(Double.parseDouble(df.format(cashManageMnets.get(i).getDepositMoney()+cashManageMnets.get(i-1).getBalance())));
                        cashManageMnets.get(i).setTotalNum(Double.parseDouble(df.format(cashManageMnets.get(i).getDepositMoney() +cashManageMnets.get(i-1).getTotalNum())) );
                    }

                }
            }
            return AjaxResult.success(cashManageMnets);
        }else{
            return AjaxResult.error();
        }

    }
}
